package sample.stackbak.symbol;

import java.util.Arrays;

/**
 * @author 陈镇雨
 */
public class Sqstacktp implements StackInft {
    /**
     * 栈的最大容量
     */
    final int maxsize;

    char elem[];
    /**
     * 栈顶“指针”
     */
    int top;

    public Sqstacktp(int maxsize) {
        this.maxsize = maxsize;
        this.elem = new char[maxsize];
        this.top = 0;
    }

    /**
     * 入栈 结点的元素
     *
     * @param obj
     */
    @Override
    public void push(Object obj) {
        if (top == maxsize) {
            System.out.println("栈溢出");
        } else {
            elem[top++] = (char) obj;
        }
    }

    /**
     * 出栈
     *
     * @return
     */
    @Override
    public Object pop() {
        if (top == 0) {
            return null;
        } else {
            top--;
            char a = elem[top];
            elem[top] = 0;
            return a;
        }
    }

    /**
     * 求出栈深
     *
     * @return
     */
    @Override
    public int size() {
        return top;
    }

    /**
     * 清空栈
     */
    @Override
    public void clear() {
        top = 0;
    }

    /**
     * 判断是否为空栈
     */
    @Override
    public void isEmpty() {
        if (top == 0) {
            System.out.println("该栈为空栈");
        } else {
            System.out.println("该栈不为空");
        }
    }

    /**
     * 读取栈顶元素
     *
     * @return
     */
    @Override
    public Object top() {
        if (top == 0) {
            return null;
        } else {
            return elem[top - 1];
        }
    }

    /**
     * 遍历栈区，Arrays.toString返回数组的字符串类型
     *
     * @return
     */
    @Override
    public String toString() {
        return "栈区{" +
                "栈的值=" + Arrays.toString(elem) +
                ", 栈的最大容量=" + maxsize +
                ", 当前栈已经用容量=" + top +
                '}';
    }
}
